
var rowIdRet = null;
var Type = null;

$(function(e){
	dataInit();
});

function dataInit(){
	eventInt();
	$('#messageTable').bootstrapTable({
		url: path+'a2b6ccbb605f4dd5a56892c574b30ee2', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset, searchParams: $("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'faultName', title: '故障名称', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'name', title: '创建人', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'createTime', title: '创建时间', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'type', title: '报修类型', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}if(value==1){
	            		value="个人报修";
	            	}if(value==2){
	            		value="公共报修"
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	            	 'click .RoleOfA': function (e, value, row, index) {
	            		 	editFault(row.id);
		                },
		                'click .RoleOfB': function (e, value, row, index) {
		                	qiao.bs.confirm("确定要删除此项吗？",function(){
			                	deleteFault(row.id);
			                },function(){});
		                },
	            },
	            formatter: function (value, row, index) {
	            	return [
						'<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">编辑</button>',
						'<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',

	        	        ].join('');
	            }
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	//修改
	$('#updatefaultForm').bootstrapValidator({
        fields: {
        	updatefaultName: {validators: {notEmpty: {message: '故障类型名称不能为空'}}},
        	updatetype: {validators: {notEmpty: {message: '报修类型不能为空'}}},
        },
        submitHandler: function (validator, form, submitButton) {
        	var params = null;
    			params = {
					rowId:rowIdRet,
					faultName:$("#updatefaultName").val(),
					type:$("#updatetype").val(),
    				
    			};
    			
    		AjaxPostUtil.request({url:path+"93524688b05f4f2e88e87ff2124509bf",params:params,type:'json',callback:function(json){
    			if(json.returnCode==0){
    				qiao.bs.msg({msg:"保存成功",type:'success'});
    				$("#updateFaultMation").modal("hide");
    				$('#messageTable').bootstrapTable('refresh',{url:path+'a2b6ccbb605f4dd5a56892c574b30ee2'});
    			}else{
    				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
    			}
    		}});
    		return false;
        }
    });
	
	$('#addfaultForm').bootstrapValidator({
	    fields: {
	    	faultName: {validators: {notEmpty: {message: '故障名称不能为空'}}},
	    	type: {validators: {notEmpty: {message: '报修类型不能为空'}}},
	    },
	    submitHandler: function (validator, form, submitButton) {
	    	var params = {
	    		faultName:$("#faultName").val(),
	    		type:$("#type").val(),
				
	    	};
	    	console.log(params);
	    	AjaxPostUtil.request({url:path+"756f9cc3ed404ef991870c4b1f1b69df",params:params,type:'json',callback:function(json){
				if(json.returnCode==0){
					qiao.bs.msg({msg:"故障类型添加成功",type:'success'});
					$("#addFaultMation").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url:path+'a2b6ccbb605f4dd5a56892c574b30ee2'});
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
			return false;
	    }
	});
	
	//删除
	function deleteFault(rowId){
		AjaxPostUtil.request({url:path+"0481a14649b84483b580639166c96924",params:{rowId:rowId},type:'json',callback:function(json){
			if(json.returnCode==0){
				qiao.bs.msg({msg:"删除成功",type:'success'});
				$('#messageTable').bootstrapTable('refresh',{url:path+'a2b6ccbb605f4dd5a56892c574b30ee2'});
			}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
		}});
	}
}




function editFault(rowId){
	AjaxPostUtil.request({url:path+"06051a2d931b4fb5b9b597d410b43b4d",params:{rowId:rowId},type:'json',callback:function(json){
		console.log(json)
		if(json.returnCode==0){
			    rowIdRet = json.bean.id;
				$("#updatefaultName").val(json.bean.faultName);
				$("#updatetype").val(json.bean.type);
				
				
				$("#updateFaultMation").modal("show");
		}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
	}});
		
}


function eventInt(){
	
	$('body').on('click', '#addFault', function(e){
		$("#addFaultMation").modal("show");
	});	

	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"a2b6ccbb605f4dd5a56892c574b30ee2", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'a2b6ccbb605f4dd5a56892c574b30ee2'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
}

